package com.ipharma.client;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.ipharma.beans.SessionUsuario;
import com.ipharma.beans.SessionUsuarioHome;
import com.ipharma.model.Usuario;

/**
 *
 */
public class SessionUsuarioClient {

    static Context ctx = null; 
    static SessionUsuarioHome home = null;

    public static void main(String[] arg) {

	// Get InitialContext
	try {
	    ctx = getInitialContext();
	} catch (NamingException e) {
	    e.printStackTrace();
	    System.exit(2);
	}

	// Lookup bean home 
	String bName = "SessionUsuarioHome";
	try {
	    home = (SessionUsuarioHome) PortableRemoteObject.narrow(ctx.lookup(bName), SessionUsuarioHome.class);
	    SessionUsuario bean = home.create();
	    bean.init();
       Usuario usuario = bean.validate("aa", "bb");
       System.out.println(usuario.getNombre() + " " + usuario.getApellido());
	} catch(Exception e) {
	    e.printStackTrace();
	    System.exit(2);
	}

	// ...
    }
    
    	/**
	 * Using a Properties object will work on JDK 1.1.x and Java2
	 * clients
	 */
	static private Context getInitialContext() throws NamingException {
      String url = "rmi://localhost:1099";
      
	  try {
		// Get an InitialContext
		Properties h = new Properties();
		h.put(Context.INITIAL_CONTEXT_FACTORY,
		  "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
		h.put(Context.PROVIDER_URL, url);
		return new InitialContext(h);
	  } catch (NamingException ne) {
	  	ne.printStackTrace();
		throw ne;
	  }
	}
    
}
